
//
//  CouponListViewController.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/3/12.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CouponListViewController.h"
#import "CustomVoucherTableViewCell.h"
#import "CouponListModel.h"
#import "CouponEmptyView.h"

@interface CouponListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property(nonatomic, strong) UIView *headView;
@property(nonatomic, strong) UIView *footView;
@property(nonatomic, strong) CouponModel *selectCouponModel;
@property(nonatomic, strong) CouponEmptyView *emptyView;
@end

@implementation CouponListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.couponListTableView.delegate = self;
    self.couponListTableView.dataSource = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.confirmBtn.layer.cornerRadius = 24.f;
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getCouponList];
}

- (void)getCouponList {
    [ApiClient getCheckoutCouponListFinishLoaded:^(CouponListModel *model, NSString *path) {
        if ([model.status isEqual:@(1)]) {
            NSMutableArray *couponArray = [NSMutableArray arrayWithArray:model.data.coupons];
            self.couponArray = couponArray;
        } else {
            self.couponArray = nil;
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
    }];
}

- (void)setCouponArray:(NSMutableArray *)couponArray {
    _couponArray = couponArray;
    if (couponArray.count == 0) {
        self.emptyView.hidden = NO;
    } else {
        self.emptyView.hidden = YES;
        [self.couponListTableView reloadData];
    }
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44.f)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 14, 80, 16)];
        label.font = [UIFont systemFontOfSize:16.f];
        label.text = @"优惠券";
        [_headView addSubview:label];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width - 44.f, 0.f, 44, 44)];
        [button setImage:[UIImage imageNamed:@"btn_shouqi"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closePageAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:button];
        
        UIView *spaceLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43.f, self.view.width, .8f)];
        spaceLine.backgroundColor = [UIColor colorWithRGB:0xe1e1e1];
        [_headView addSubview:spaceLine];
        _headView.backgroundColor=[UIColor whiteColor];
    }
    return _headView;
}


- (CouponEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[CouponEmptyView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.width)];
        [self.view addSubview:_emptyView];
        [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(@(10));
            make.size.mas_equalTo(CGSizeMake(300, 300));
        }];
    }
    return _emptyView;
}

- (UIView *)footView {
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44.f)];
    }
    return _footView;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.couponArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CouponListCell";
    CustomVoucherTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                        identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomVoucherTableViewCell" owner:nil options:nil] lastObject];
        cell.enableSelect = YES;
    }
    
    cell.selectCouponBlock = ^(CouponModel *model) {
        self.selectCouponModel = model;
        if (model.isChecked) {
            for (CouponModel *couponModel in self.couponArray) {
                if (model != couponModel) {
                    couponModel
                    .isChecked = NO;
                }
            }
        }
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CouponModel *model = [self.couponArray objectAtIndex:indexPath.row];
    [cell updateCouponInfo:model];
    [cell reshCouponState:model selected:self.couponCode];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.footView;
}

- (void)closePageAction:(id)sender {
    if (self.closeCouponPageBlock) {
        self.closeCouponPageBlock();
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomVoucherTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    [cell chooseCouponAction:cell.chooseBtn];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)confirmAction:(id)sender {
    if (!self.selectCouponModel) {
        [ApiClient deleteShippingCouponDidFinishLoaded:^(KXJson *json, NSString *path) {
            self.selectCouponModel = nil;
            if ([[json getStringForKey:@"status"] isEqualToString:@"1"]) {
                if (self.closeCouponPageBlock) {
                    self.closeCouponPageBlock();
                }
                if (self.chooseCouponBlock) {
                    self.chooseCouponBlock(self.selectCouponModel);
                }
            }
        } didFailLoaded:^(NSError *error, NSString *path) {
            
        }];
    } else {
        [ApiClient setShippingCoupon:self.selectCouponModel.code didFinishLoaded:^(KXJson *json, NSString *path) {
            if ([[json getStringForKey:@"status"] isEqualToString:@"1"]) {
                if (self.closeCouponPageBlock) {
                    self.closeCouponPageBlock();
                }
                if (self.chooseCouponBlock) {
                    self.chooseCouponBlock(self.selectCouponModel);
                }
            }
        } didFailLoaded:^(NSError *error, NSString *path) {
            
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
